package com.supermarket.model;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

/**
 * 商品实体类
 * 对应数据库中的products表
 */
public class Product {
    private int id;
    private String productCode;
    private String productName;
    private int categoryId;
    private String categoryName; // 类别名称（用于显示）
    private int supplierId;
    private String supplierName; // 供应商名称（用于显示）
    private String unit;
    private BigDecimal purchasePrice;
    private BigDecimal salePrice;
    private Integer shelfLifeDays;
    private String description;
    private String status;
    private Timestamp createdAt;
    private Timestamp updatedAt;

    // 库存相关信息（从inventory表中获取）
    private BigDecimal currentStock;
    private BigDecimal minStock;
    private BigDecimal maxStock;
    private String stockStatus;
    private java.sql.Date lastSaleDate; // 最后销售日期

    // 临期商品相关信息
    private Integer remainingDays; // 剩余保质期天数
    private Date productionDate; // 生产日期，用于计算保质期
    private Date expiryDate; // 过期日期
    private String expiryStatus; // 过期状态

    // 构造函数
    public Product() {
    }

    public Product(String productCode, String productName, String categoryName,
            BigDecimal salePrice, BigDecimal currentStock) {
        this.productCode = productCode;
        this.productName = productName;
        this.categoryName = categoryName;
        this.salePrice = salePrice;
        this.currentStock = currentStock;
    }

    // Getter和Setter方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProductCode() {
        return productCode;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public int getSupplierId() {
        return supplierId;
    }

    public void setSupplierId(int supplierId) {
        this.supplierId = supplierId;
    }

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public BigDecimal getPurchasePrice() {
        return purchasePrice;
    }

    public void setPurchasePrice(BigDecimal purchasePrice) {
        this.purchasePrice = purchasePrice;
    }

    public BigDecimal getSalePrice() {
        return salePrice;
    }

    public void setSalePrice(BigDecimal salePrice) {
        this.salePrice = salePrice;
    }

    public Integer getShelfLifeDays() {
        return shelfLifeDays;
    }

    public void setShelfLifeDays(Integer shelfLifeDays) {
        this.shelfLifeDays = shelfLifeDays;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Timestamp getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Timestamp createdAt) {
        this.createdAt = createdAt;
    }

    public Timestamp getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Timestamp updatedAt) {
        this.updatedAt = updatedAt;
    }

    public BigDecimal getCurrentStock() {
        return currentStock;
    }

    public void setCurrentStock(BigDecimal currentStock) {
        this.currentStock = currentStock;
    }

    public BigDecimal getMinStock() {
        return minStock;
    }

    public void setMinStock(BigDecimal minStock) {
        this.minStock = minStock;
    }

    public BigDecimal getMaxStock() {
        return maxStock;
    }

    public void setMaxStock(BigDecimal maxStock) {
        this.maxStock = maxStock;
    }

    public String getStockStatus() {
        return stockStatus;
    }

    public void setStockStatus(String stockStatus) {
        this.stockStatus = stockStatus;
    }

    public java.sql.Date getLastSaleDate() {
        return lastSaleDate;
    }

    public void setLastSaleDate(java.sql.Date lastSaleDate) {
        this.lastSaleDate = lastSaleDate;
    }

    public Integer getRemainingDays() {
        return remainingDays;
    }

    public void setRemainingDays(Integer remainingDays) {
        this.remainingDays = remainingDays;
        // 根据剩余天数自动更新过期状态
        if (remainingDays == null) {
            this.expiryStatus = "信息不完整";
        } else if (remainingDays <= 0) {
            this.expiryStatus = "已过期";
        } else if (remainingDays <= 3) {
            this.expiryStatus = "即将过期";
        } else if (remainingDays <= 7) {
            this.expiryStatus = "临期商品";
        } else if (remainingDays <= 30) {
            this.expiryStatus = "即将临期";
        } else {
            this.expiryStatus = "正常";
        }
    }

    public Date getProductionDate() {
        return productionDate;
    }

    public void setProductionDate(Date productionDate) {
        this.productionDate = productionDate;
    }

    public Date getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(Date expiryDate) {
        this.expiryDate = expiryDate;
    }

    public String getExpiryStatus() {
        return expiryStatus;
    }

    public void setExpiryStatus(String expiryStatus) {
        this.expiryStatus = expiryStatus;
    }

    /**
     * 判断是否有库存
     */
    public boolean hasStock() {
        return currentStock != null && currentStock.compareTo(BigDecimal.ZERO) > 0;
    }

    /**
     * 判断是否为低库存
     */
    public boolean isLowStock() {
        return "低库存".equals(stockStatus) || "缺货".equals(stockStatus);
    }

    /**
     * 获取利润
     */
    public BigDecimal getProfit() {
        if (salePrice != null && purchasePrice != null) {
            return salePrice.subtract(purchasePrice);
        }
        return BigDecimal.ZERO;
    }

    /**
     * 获取利润率（百分比）
     */
    public BigDecimal getProfitRate() {
        if (purchasePrice != null && purchasePrice.compareTo(BigDecimal.ZERO) > 0 && salePrice != null) {
            return salePrice.subtract(purchasePrice)
                    .divide(purchasePrice, 4, java.math.RoundingMode.HALF_UP)
                    .multiply(new BigDecimal("100"));
        }
        return BigDecimal.ZERO;
    }

    /**
     * 计算过期日期
     * 根据出厂日期和保质期天数计算过期日期
     */
    public void calculateExpiryDate() {
        if (productionDate != null && shelfLifeDays != null) {
            java.util.Calendar calendar = java.util.Calendar.getInstance();
            calendar.setTime(productionDate);
            calendar.add(java.util.Calendar.DAY_OF_MONTH, shelfLifeDays);
            this.expiryDate = new java.sql.Date(calendar.getTimeInMillis());
        }
    }

    /**
     * 计算剩余天数
     * 根据过期日期和当前日期计算剩余天数
     */
    public void calculateRemainingDays() {
        if (expiryDate != null) {
            java.util.Date currentDate = new java.util.Date();
            long diffInMillies = expiryDate.getTime() - currentDate.getTime();
            this.remainingDays = (int) (diffInMillies / (1000 * 60 * 60 * 24));
        }
    }

    /**
     * 判断过期状态
     * 根据剩余天数判断商品的过期状态
     */
    public void determineExpiryStatus() {
        if (shelfLifeDays == null || productionDate == null) {
            this.expiryStatus = "信息不完整";
            return;
        }

        // 确保过期日期和剩余天数已计算
        if (expiryDate == null) {
            calculateExpiryDate();
        }
        if (remainingDays == null) {
            calculateRemainingDays();
        }

        if (remainingDays == null) {
            this.expiryStatus = "信息不完整";
        } else if (remainingDays <= 0) {
            this.expiryStatus = "已过期";
        } else if (remainingDays <= 3) {
            this.expiryStatus = "即将过期";
        } else if (remainingDays <= 7) {
            this.expiryStatus = "临期商品";
        } else if (remainingDays <= 30) {
            this.expiryStatus = "即将临期";
        } else {
            this.expiryStatus = "正常";
        }
    }

    /**
     * 判断是否为临期商品
     * 剩余保质期少于等于7天的商品被认为是临期商品
     */
    public boolean isExpiring() {
        return remainingDays != null && remainingDays <= 7 && remainingDays > 0;
    }

    /**
     * 判断是否已过期
     * 剩余保质期小于等于0天的商品被认为是已过期
     */
    public boolean isExpired() {
        return remainingDays != null && remainingDays <= 0;
    }

    /**
     * 判断是否即将过期
     * 剩余保质期少于等于3天的商品被认为是即将过期
     */
    public boolean isAboutToExpire() {
        return remainingDays != null && remainingDays <= 3 && remainingDays > 0;
    }

    /**
     * 获取过期状态的颜色代码（用于UI显示）
     */
    public String getExpiryStatusColor() {
        if (expiryStatus == null) {
            return "#ffffff"; // 白色
        }

        switch (expiryStatus) {
            case "已过期":
                return "#ffebee"; // 红色背景
            case "即将过期":
                return "#fff3e0"; // 橙色背景
            case "临期商品":
                return "#fffde7"; // 黄色背景
            case "即将临期":
                return "#e3f2fd"; // 浅蓝色背景
            case "正常":
                return "#ffffff"; // 白色背景
            case "信息不完整":
                return "#f5f5f5"; // 灰色背景
            default:
                return "#ffffff"; // 白色背景
        }
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", productCode='" + productCode + '\'' +
                ", productName='" + productName + '\'' +
                ", categoryName='" + categoryName + '\'' +
                ", supplierName='" + supplierName + '\'' +
                ", unit='" + unit + '\'' +
                ", salePrice=" + salePrice +
                ", currentStock=" + currentStock +
                ", stockStatus='" + stockStatus + '\'' +
                ", productionDate=" + productionDate +
                ", expiryDate=" + expiryDate +
                ", remainingDays=" + remainingDays +
                ", expiryStatus='" + expiryStatus + '\'' +
                '}';
    }
}
